package com.zjun.demo.experimentaldemo.notification

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import com.zjun.demo.experimentaldemo.R

object NotificationUtil {

    @Suppress("DEPRECATION")
    fun notify(
        context: Context,
        channelID: String,
        channelName: String,
        title:CharSequence,
        text:CharSequence,
        intent: Intent,
        smallIcon:Int = R.drawable.audiomute,
        requestCode:Int = 0,
        id:Int = 100,
        priority:Int = NotificationCompat.PRIORITY_DEFAULT,
    ) {
        val pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE )
        val channelId = createNotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH,context)
        val notification = if (channelId == null) {
            NotificationCompat.Builder(context)
        } else {
            NotificationCompat.Builder(context, channelId)
        }

        val soundUri = Uri.parse("android.resource://${context.packageName}" + "/" + R.raw.hazard_warning)


        notification.setContentTitle(title)
            .setContentText(text)
            .setContentIntent(pendingIntent)
            .setSmallIcon(smallIcon)
            .setLargeIcon(ContextCompat.getDrawable(context,smallIcon)!!.toBitmap())
            .setPriority(priority)
            .setVisibility(VISIBILITY_PUBLIC)
            .setSound(null)
            .setAutoCancel(true)
        val notificationManager = NotificationManagerCompat.from(context)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
                notificationManager.notify(id, notification.build())
            }
        } else {
            notificationManager.notify(id, notification.build())
        }
    }
    private fun createNotificationChannel(channelID: String, channelName: String, level: Int, context: Context): String? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val manager = context.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
            val channel = NotificationChannel(channelID, channelName, level)
            manager.createNotificationChannel(channel)
            channelID
        } else {
            null
        }
    }

}